let musicList = []
// 表示当前正在播放的index
let nowPlayingIndex = 0
// 获取全局唯一的背景音频管理器管理器
const backgroundAudioManager = wx.getBackgroundAudioManager()
// 获取全局方法
const app = getApp()
Page({
  data: {
    picUrl: '',
    isPlaying: false,   //标识当前的播放状态，为true表示播放，为false表示暂停
    isLyricShow: false,  //表示当前歌词是否显示
    lyric: '',   //歌词
    isSame: false  //定义两次点击的是否是同一首歌曲
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    nowPlayingIndex = options.index
    musicList = wx.getStorageSync('musiclist')
    this._loadMusicDetail(options.musicId)
  },
  // 加载当前需要播放的音乐函数
 _loadMusicDetail(musicId){
   if(musicId == app.getPlayingMusicId()){
     this.setData({
       isSame: true
     })
   }else{
     this.setData({
       isSame: false
     })
   }
   if(!this.data.isSame){
     backgroundAudioManager.stop()
   }
   
   let music = musicList[nowPlayingIndex]
   wx.showLoading({
     title: '加载中'
   })
    wx.setNavigationBarTitle({
      title: music.name
    })
    this.setData({
      picUrl: music.al.picUrl,
      isPlaying: false
    })
    app.setPlayingMusicId(musicId)
    wx.cloud.callFunction({
      name: 'music',
      data: {
        $url: 'musicUrl',
        musicId
      }
    }).then((res) => {
      let result = JSON.parse(res.result)
      if(result.data[0].url == null){
        wx.showToast({
          title: '无权限播放'
        })
        return
      }
      if(!this.data.isSame){
        backgroundAudioManager.src = result.data[0].url
        backgroundAudioManager.title = music.name
        backgroundAudioManager.coverImgUrl = music.al.picUrl
        backgroundAudioManager.singer = music.ar[0].name
        backgroundAudioManager.epname = music.al.name

        //保存播放历史
        this.saveplayHistory()
      }
      this.setData({
        isPlaying: true
      })
      wx.hideLoading()
      // 这里开始加载歌词
      wx.cloud.callFunction({
        name: 'music',
        data: {
          $url: 'lyric',
          musicId
        }
      }).then((res) => {
        let lyric = '暂无歌词'
        const lrc = JSON.parse(res.result).lrc
        if(lrc){
          lyric = lrc.lyric
        }
        this.setData({
          lyric
        })
      })
    })
 },
// 切换暂停播放
 togglePlaying(){
   if(this.data.isPlaying){
     backgroundAudioManager.pause()
   }else{
     backgroundAudioManager.play()
   }
   this.setData({
     isPlaying: !this.data.isPlaying
   })
 },
//  上一首播放函数
 onPrev(){
   nowPlayingIndex--
   if (nowPlayingIndex < 0){
     nowPlayingIndex = musicList.length - 1
   }
   this._loadMusicDetail(musicList[nowPlayingIndex].id)
 },
//  下一首播放函数
 onNext(){
   nowPlayingIndex++
   if(nowPlayingIndex === musicList.length) {
     nowPlayingIndex = 0
   }
   this._loadMusicDetail(musicList[nowPlayingIndex].id)
 },
// 切换个歌词函数定义
onChangeLyricShow(){
  this.setData({
    isLyricShow: !this.data.isLyricShow
  })
},
timeUpdate(event){
  this.selectComponent('.lyric').update(event.detail.currentTime)
},
// 播放函数
onPlay(){
  this.setData({
    isPlaying: true
  })
},
// 暂停函数
onPause(){
  this.setData({
    isPlaying: false
  })
},

// 保存播方信息
saveplayHistory(){
  const music = musicList[nowPlayingIndex]
  const openid = app.globalData.openid
  const history = wx.getStorageSync(openid)
  let bHave = false
  for(let i = 0, len = history.length; i < len; i++){
    if(history[i].id == music.id){
      bHave = true
      break
    }
  }
  if(!bHave){
    history.unshift(music)
    wx.setStorage({
      key: openid,
      data: history
    })
  }
},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})